package com.fengpanlong.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.fengpanlong.entity.Task;
import com.fengpanlong.util.DBHelp;
import com.fengpanlong.util.RowMapper;

public class TaskDao {
	private DBHelp<Task> db = new DBHelp<Task>();
	
	private class TaskRowMapper implements RowMapper<Task> {

		public Task mapperRow(ResultSet rs) throws SQLException {
			Task t = new Task();
			t.setId(rs.getString("id"));
			t.setName(rs.getString("name"));
			t.setDesc(rs.getString("desc"));
			t.setState(rs.getString("state"));
			t.setLevel(rs.getString("level"));
			t.setBegintime(rs.getString("begintime"));
			t.setEndtime(rs.getString("endtime"));
			t.setCreatetime(rs.getString("createtime"));
			t.setRate(rs.getString("rate"));
			t.setEmployeeId(rs.getString("employeeId"));
			t.setGogalId(rs.getString("gogalId"));
			return t;
		}
	}
	/**
	 * 通过目标id查找任务列表
	 * @param gogalId 目标id
	 * @return 任务列表
	 */
	public List<Task> findTasksByGogalId(String gogalId) {
		String sql = "select id,name,`desc`,state,level,begintime,endtime,createtime,rate,employeeid,gogalid from t_task where gogalid = ?";
		return db.executeQueryForList(sql, new TaskRowMapper(), gogalId);
	}
	/**
	 * 通过用户id和项目id查找任务列表
	 * @param employeeId 用户id
	 * @param projectId 项目id
	 * @return 任务列表
	 */
	public List<Task> findTasksByEmployeeIdAndByProjectId(String employeeId,
			String projectId) {
		String sql = "select id,name,`desc`,state,level,begintime,endtime,createtime,rate,employeeid,gogalid from t_task where employeeid = ? and gogalid in (select gogalid from t_gogal where projectid = ?) order by level";
		return db.executeQueryForList(sql, new TaskRowMapper(), employeeId,projectId);
	}
	/**
	 * 通过任务id查找任务
	 * @param taskId 任务id
	 * @return 任务
	 */
	public Task findTaskByTaskId(String taskId) {
		String sql = "select id,name,`desc`,state,level,begintime,endtime,createtime,rate,employeeid,gogalid from t_task where id = ?";
		return db.executeQueryForObject(sql, new TaskRowMapper(), taskId);
	}
	/**
	 * 更新任务
	 * @param task
	 */
	public void updateTask(Task t) {
		String sql = "update t_task set name=?,`desc`=?,state=?,level=?,begintime=?,endtime=?,createtime=?,rate=?,employeeid=?,gogalid=? where id = ?";
		db.executeSQL(sql, t.getName(),t.getDesc(),t.getState(),t.getLevel(),t.getBegintime(),t.getEndtime(),t.getCreatetime(),t.getRate(),t.getEmployeeId(),t.getGogalId(),t.getId());
	}
	/**
	 * 保存任务
	 * @param task
	 */
	public void saveTask(Task t) {
		// TODO Auto-generated method stub
		String sql = "insert into t_task(id,name,`desc`,state,level,begintime,endtime,createtime,rate,employeeid,gogalid) values(?,?,?,?,?,?,?,?,?,?,?)";
		db.executeSQL(sql, t.getId(),t.getName(),t.getDesc(),t.getState(),t.getLevel(),t.getBegintime(),t.getEndtime(),t.getCreatetime(),t.getRate(),t.getEmployeeId(),t.getGogalId());
	}
	/**
	 * 通过目标id删除任务
	 * @param gogalId
	 */
	public void delTaskByGogalId(String gogalId) {
		String sql = "delete from t_task where gogalid = ?";
		db.executeSQL(sql, gogalId);
	}
	/**
	 * 通过任务id删除任务
	 * @param taskId
	 */
	public void delTaskByTaskId(String taskId) {
		String sql = "delete from t_task where id = ?";
		db.executeSQL(sql, taskId);
	}
}
